package com.tadpole.music.presenter.question;

import com.tadpole.music.bean.question.UniversityBean;
import com.tadpole.music.callback.BaseCallback;
import com.tadpole.music.config.Constant;
import com.tadpole.music.config.UrlConfig;
import com.tadpole.music.iView.question.UniversityIView;
import com.tadpole.music.model.base.DataModel;
import com.tadpole.music.model.base.ModelToken;
import com.tadpole.music.presenter.base.BasePresenter;
import com.tadpole.music.utils.JsonParseUtil;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 获取题库中的院校
 * Created By 孙岩
 * on 2020/9/15
 */
public class UniversityPresenter extends BasePresenter<UniversityIView> {
    @SuppressWarnings("unchecked")
    public void getUniversity() {
        if (isViewAttached()) {
            String url = UrlConfig.getUniversity + "?college_type=-1";
            String token = SpUtil.getInstance(getView().getContext()).getString(Constant.TOKEN, "");
            DataModel.request(ModelToken.GET_HEAD_REQUEST)
                    .url(url)
                    .token(token)
                    .execute(new BaseCallback<String>() {
                        @Override
                        public void onSuccess(String data) {
                            try {
                                JSONObject obj = new JSONObject(data);
                                if (obj.getInt("code") == 200) {
                                    JSONArray array = new JSONArray(obj.getString("data"));
                                    List<UniversityBean.DataBean> list = new ArrayList<>();
                                    for (int i = 0; i < array.length(); i++) {
                                        JSONObject object = new JSONObject(array.get(i).toString());
                                        List<UniversityBean.DataBean.CollegeFunctionsBean> list1 = new ArrayList<>();

                                        if(object.isNull("college_functions")){
                                            list1 = null;
                                        } else{
                                            JSONArray jsonArray = new JSONArray(object.getString("college_functions"));
                                            for (int j = 0; j < jsonArray.length(); j++) {
                                                JSONObject jsonObject = new JSONObject(jsonArray.get(j).toString());
                                                UniversityBean.DataBean.CollegeFunctionsBean bean = new UniversityBean.DataBean.CollegeFunctionsBean(
                                                        JsonParseUtil.getInt(jsonObject, "id"),
                                                        JsonParseUtil.getStr(jsonObject, "name"),
                                                        JsonParseUtil.getStr(jsonObject, "thumb")
                                                );
                                                list1.add(bean);
                                            }
                                        }

                                        UniversityBean.DataBean dataBean = new UniversityBean.DataBean(
                                                JsonParseUtil.getInt(object, "id"),
                                                JsonParseUtil.getStr(object, "name"),
                                                JsonParseUtil.getStr(object, "chief"),
                                                JsonParseUtil.getStr(object, "short_name"),
                                                list1
                                        );
                                        list.add(dataBean);
                                    }

                                    getView().showUniversity(list);
                                } else if(obj.getInt("code")==401){
                                    getView().show401();
                                }
                                else {
                                    ToastUtils.show(obj.getString("msg"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(String msg) {
                            getView().showErr();
                        }

                        @Override
                        public void onComplete() {
                            getView().hideLoading();
                        }

                        @Override
                        public void onBefore() {
                            getView().showLoading();
                        }
                    });
        }
    }
}
